<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

if($exec == 1){
	$cTheme->SetTitle("Register");
	if(isset($_POST['rsubmit']) && $_POST['rsubmit'] == "Register"){
		$k = false;
		$buffer = false;
		
		if(!(isset($_POST['rname']) && strlen($_POST['rname']) >= 3)){
			$buffer = "<li>Incorrect username format</li>";
		}else {
			if(!preg_match("/^[a-zA-Z ]+$/", $_POST['rname'])){ $buffer = "<li>Incorrect username format</li>"; }
		}
		if(isset($_POST['rpass']) && strlen($_POST['rpass']) >= 5){
			if($_POST['rpass'] != $_POST['rpassc']){ $buffer .= "<li>Passwords do not mach.</li>"; }
		}else { $buffer .= "<li>Incorrect password format</li>"; }
		
		if(!checkemail($_POST['rmail'])){$buffer .= "<li>Incorrect email format.</li>"; }
		if($_SESSION["rvcode"] != $_POST['rverif']){ $buffer .= "<li>Incorrect verification code.</li>"; }
		
		if($q->Rows($q->Send("SELECT * FROM users WHERE username=\"".$_POST['rname']."\"")) != 0){
			$buffer .= "<li>User with that name already exist.</li>";
		}
		
		if($buffer != false){
			$cTheme->AddModuleError("<b>You were not able to register because of these reasons:</b>".$buffer);
		}else if($buffer == false){
			$key = generateRandomSring(128, 3);
			$mops = $q->GetOp("user_register_mail_title", "user_register_mail_content", "core_link_full", "core_link");
			mail($_POST['rmail'], $mops[0]['value'], str_replace("{{APLINK}}", "<a href=\"{$mops[2]['value']}{$mops[3]['value']}/mail.validate.php?uid=$key\">{$mops[2]['value']}{$mops[3]['value']}</a>", $mops[1]['value']));
			$q->Send("INSERT INTO users (username, password, settings, flags, regtime, email, ip, other, mail_confirmed, mail_uid) VALUES (\"".$q->Escape($_POST['rname'])."\", \"".hash("sha512", $_POST['rpass'])."\", \"[]\", \"u\", \"".time()."\", \"".$q->Escape($_POST['rmail'])."\", \"{$_SERVER['REMOTE_ADDR']}\", \"[]\", \"0\", \"$key\")")or die(mysql_error());
			
			$k = true;
			$cTheme->AddModuleNotification("Your registration was a success. Please note that you need to activate your account by clicking the link in the email we have sent you. You should receive email message within 5 minutes.");
		}
	}
	if(!$k){
		$buffer  = "<form method=\"post\" action=\"\">";
		$buffer .= "<table>";
		$buffer .= "<tr><td align=\"right\">Username:</td><td><input type=\"text\" name=\"rname\" value=\"".(isset($_POST['rname']) ? $_POST['rname']:"")."\" /></td></tr>";
		$buffer .= "<tr><td align=\"right\">Password:</td><td><input type=\"password\" name=\"rpass\" /></td></tr>";
		$buffer .= "<tr><td align=\"right\">Repeat password:</td><td><input type=\"password\" name=\"rpassc\" /></td></tr>";
		$buffer .= "<tr><td align=\"right\">Email address:</td><td><input type=\"text\" name=\"rmail\" value=\"".(isset($_POST['rmail']) ? $_POST['rmail']:"")."\" /></td></tr>";
		$buffer .= "<tr><td align=\"right\" valign=\"top\">Verify:</td><td><img src=\"includes/others/captcha.php\" /><br /><input type=\"text\" class=\"rc\" name=\"rverif\" size=\"4\" maxlength=\"4\" /></td></tr>";
		$buffer .= "<tr><td align=\"right\">&nbsp;</td><td><input type=\"submit\" class=\"button\" name=\"rsubmit\" value=\"Register\" /></td></tr>";
		$buffer .= "</table>";
		$buffer .= "</form>";
		$cTheme->AddModule("Registration page", $buffer, 10)or die($cTheme->Error(__LINE__));
	}
}else { header("Location: /"); } 